#!/usr/bin/python
import sys


__author__ = 'meirdrago'

class Comparer(object):

        #       objSlave - THE NEW OBJECT .  objMaster - THE COMPARABLE OBJECT
        #       masterFactor - CONSTANT INTEGER TO DEVIDE THE MASTER FIELD VALUE
        def __init__(self,conf, objMaster , objSlave , masterFactor = 1 ):
                self.conf = conf
                self.masterFactor = float(masterFactor)
                self.objMaster = objMaster
                self.objSlave = objSlave
                self.Persist = {}
                self.report = {"lowercount":0 , "notexists":0 , "marked":0}

        #       in normal mode marked results will be set as field key and slave value
        #       if verbose = True than results will be set as field key and [slave value , Master value]
        def compare(self , verbose=False):

                for k in self.objMaster:

                        #       DID NOT REACH TO MIN COUNT
                        if self.objMaster[k]["count"]   < self.conf["minval"] :
                                self.report["lowercount"] += 1
                                continue

                        row = {}
                        #       DOES NOT EXISTS IN THE SALVE OBJECT - WRITE ALL FIELDS AS 0
                        if not k in self.objSlave :
                                for f in self.objMaster[k]:
                                        if self.objMaster[k][f] == 0 : continue
                                        row[f] = 0.0 if verbose == False else [0.0 , self.objMaster[k][f] ]
                                self.Persist[k] = row
                                self.report["notexists"] += 1
                        else :
                                for f in self.objMaster[k]:
                                        if self.objMaster[k][f] == 0 : continue
					try:	# 	for any malformed surprise
						if self.conf["rates"][f] < 1.0 :
		                                        if self.objSlave[k][f] == 0 or self.objSlave[k][f] / self.objMaster[k][f]  < self.conf["rates"][f] :
        		                                        row[f]  = self.objSlave[k][f] if verbose == False else [self.objSlave[k][f] , (self.objMaster[k][f] / self.masterFactor)]
						else :
                                                        if self.objSlave[k][f] == 0 or self.objSlave[k][f] / self.objMaster[k][f]  > self.conf["rates"][f] :
                                                                row[f]  = self.objSlave[k][f] if verbose == False else [self.objSlave[k][f] , (self.objMaster[k][f] / self.masterFactor)]
					except : pass
                                if len(row.keys()) > 0 :
                                        self.Persist[k] = row
                                        self.report["marked"] += 1

############################################################
